package br.com.app.listagem;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.faces.bean.ManagedBean;

@ManagedBean
public class TableBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6020185981517439182L;

	private final static String[] colors;

	private final static String[] manufacturers;

	static {
		colors = new String[10];
		colors[0] = "Aberto";
		colors[1] = "Pago";
		colors[2] = "Fechado";
		colors[3] = "Aberto";
		colors[4] = "Pago";
		colors[5] = "Fechado";
		colors[6] = "Aberto";
		colors[7] = "Aberto";
		colors[8] = "Aberto";
		colors[9] = "Fechado";

		manufacturers = new String[10];
		manufacturers[0] = "Mercedes";
		manufacturers[1] = "BMW";
		manufacturers[2] = "Volvo";
		manufacturers[3] = "Audi";
		manufacturers[4] = "Renault";
		manufacturers[5] = "Opel";
		manufacturers[6] = "Volkswagen";
		manufacturers[7] = "Chrysler";
		manufacturers[8] = "Ferrari";
		manufacturers[9] = "Ford";
	}

	private List<Car> cars;

	private Car selectedCar;

	private Car[] selectedCars;

	private CarDataModel mediumCarsModel;

	public TableBean() {
		cars = new ArrayList<Car>();

		populateRandomCars(cars, 50);

		mediumCarsModel = new CarDataModel(cars);
	}

	public Car[] getSelectedCars() {
		return selectedCars;
	}

	public void setSelectedCars(Car[] selectedCars) {
		this.selectedCars = selectedCars;
	}

	public Car getSelectedCar() {
		return selectedCar;
	}

	public void setSelectedCar(Car selectedCar) {
		this.selectedCar = selectedCar;
	}

	private void populateRandomCars(List<Car> list, int size) {
		for (int i = 0; i < size; i++)
			list.add(new Car(getRandomModel(), getRandomYear(),
					getRandomManufacturer(), getRandomColor()));
	}

	private int getRandomYear() {
		return (int) (Math.random() * 50 + 1960);
	}

	private String getRandomColor() {
		return colors[(int) (Math.random() * 10)];
	}

	private String getRandomManufacturer() {
		return manufacturers[(int) (Math.random() * 10)];
	}

	private String getRandomModel() {
		return UUID.randomUUID().toString().substring(0, 8);
	}

	public CarDataModel getMediumCarsModel() {
		return mediumCarsModel;
	}
}
